<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import * as echarts from "echarts";
require("echarts/theme/macarons"); // echarts theme
import resize from "./mixins/resize";

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: "chart",
    },
    width: {
      type: String,
      default: "100%",
    },
    height: {
      type: String,
      default: "100%",
    },
    autoResize: {
      type: Boolean,
      default: true,
    },
    radartData: {
      type: Object,
      required: true,
    },
  },
  data() {
    return {
      chart: null,
    };
  },
  watch: {
    radartData: {
      deep: true,
      handler(val) {
        if (val.series && val.max) {
          this.setOptions(val);
        }
      },
    },
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart();
    });
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el);
      this.setOptions(this.radartData);
    },
    setOptions({ xAxis, series, legend, color, yAxis, title, grid, tooltip }) {
      let option = {
        grid: {
          top: "20%",
          left: "2%",
          right: "3%",
          bottom: "5%",
          containLabel: true,
        },
        color: [
          {
            type: "radial",
            x: 0.5,
            y: 0.5,
            r: 0.5,
            colorStops: [
              {
                offset: 0,
                color: "#7EFBFF", // 0% 处的颜色
              },
              {
                offset: 1,
                color: "#529EFE", // 100% 处的颜色
              },
            ],
            global: false, // 缺省为 false
          },
        ],
        radar: {
          radius: "60%",
          shape: "circle",
          splitNumber: 4,
          splitArea: {
            areaStyle: {
              color: ["#55B59B", "#216D5E", "#164C44", "#0E302C"],
              shadowColor: "rgba(0, 0, 0, 0.2)",
              shadowBlur: 10,
            },
          },
          name: {
            formatter: function (value, indicator) {
              return value;
            },
            textStyle: {
              color: "#fff",
            },
          },
          axisLine: {
            show: false,
            lineStyle: {
              color: "rgba(255, 255, 255, 0.4)",
            },
          },
          splitLine: {
            show: false,
            lineStyle: {
              color: "rgba(255, 255, 255, 0.4)",
            },
          },
          indicator: [
            { name: "已提交", max: this.radartData.max },
            { name: "调研中", max: this.radartData.max },
            { name: "开发中", max: this.radartData.max },
            { name: "测试中", max: this.radartData.max },
            { name: "试运行", max: this.radartData.max },
            { name: "需求上线", max: this.radartData.max },
          ],
        },
        series: [
          {
            type: "radar",
            data: [
              {
                label: {
                  show: true,
                  color: "#fff",
                  formatter: function (params) {
                    return params.value;
                  },
                },
                value: this.radartData.series,
                areaStyle: {
                  color: {
                    type: "radial",
                    x: 0.5,
                    y: 0.5,
                    r: 0.5,
                    colorStops: [
                      {
                        offset: 0,
                        color: "#7EFBFF", // 0% 处的颜色
                      },
                      {
                        offset: 1,
                        color: "#529EFE", // 100% 处的颜色
                      },
                    ],
                    global: false, // 缺省为 false
                  },
                },
              },
            ],
          },
        ],
      };
      this.chart.setOption(option);
    },
  },
};
</script>
